package com.wwwc.index.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.ejb.EJBManager;
import com.wwwc.index.ejb.directory.*;

public class DirectoryServlet extends HttpServlet {
    private Directory ejbdb;

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	
	try {
	    response.setContentType("text/html");

	    String user_ip       = request.getRemoteAddr();
	    HttpSession session  = request.getSession(true);
	    PrintWriter out      = response.getWriter();
	    String context       = request.getContextPath();
	    String call_path     = request.getContextPath()+request.getServletPath();
	   
	    String lid = (String)session.getAttribute("SelectedLanguageId");
	    if(lid == null) {
		System.out.println("DirectoryServlet:Error:lid==null");
		return;
	    }

	    CategoryDetails cd = (CategoryDetails)session.getAttribute("SelectedCategory");
	    if(cd == null) {
		System.out.println("DirctroryServlet:Error:SelectedCategory==null");
		return;
	    }

	    String cid = ""+cd.getCategoryId();

	    String directory_table_name = cd.getDirectoryTableName();

	    if(!lid.equals("en")) {
		directory_table_name = directory_table_name+"_"+lid.toUpperCase();
	    }

	    if (ejbdb == null) {
		ejbdb = (Directory)EJBManager.getEJB("java:comp/env/ejb/DirectoryEJB", "Directory");
	    }

	    UserProfile user_profile = (UserProfile)session.getAttribute("UserProfile");

	    //=========================================================================
	    // Create directory table
	    //=========================================================================
	    
	    if(directory_table_name != null && ejbdb.tableExists(directory_table_name) == 0) {
		if(user_profile != null && user_profile.getUserLevel()>=99) {
		    ejbdb.createDirectoryTable(directory_table_name);
		    ejbdb.insertDefaultDirectoryValue(directory_table_name, context, 
						      cd.getCategoryId(), cd.getCategoryName(), 
						      user_profile.getUserName());
		}
	    }

	    Hashtable mhs = (Hashtable)session.getAttribute("LanguageHash_"+lid);
	    if(mhs == null) {
		return;
	    }

	    String did = request.getParameter("did");
	   
	    if(did != null) {
		session.setAttribute("did", did);
	    }

	    String mid = request.getParameter("mid");

	    if(mid == null) {
		mid = (String)session.getAttribute("LastMainPageMId");
	    }
	    else {
		session.setAttribute("LastMainPageMId", mid);
	    }

	    ////////////////////////////////////////////////////////////////////////////////
	    // controller 
	    ////////////////////////////////////////////////////////////////////////////////

	    out.println("<BR>");
	    out.println("<TABLE width=100%>");

	    if(user_profile != null) {
		out.println("<TR><TD align=center>");
		out.println("<table cellSpacing=1 cellPadding=0 width=80% border=0>");
		out.println("<tr>");
		out.println("<td align=center style='BORDER:1px solid'>"+user_profile.getUserName());
		out.println("</td></tr>");
		out.println("</table>");
		out.println("</TD></TR>");
		out.println("<TR><TD align=center>");
		out.println("<table cellSpacing=1 cellPadding=0 width=80% border=0>");
		out.println("<tr>");
		out.println("<td align=center style='BORDER:1px solid'>");
		out.println("<A href="+call_path+"?mid=sign_off style='text-decoration: none;'>");
		out.println(LanguageTrans.getString(session, lid, "Sign Off", mhs)+"</A>");
		out.println("</td></tr>");
		out.println("</table>");
		out.println("</TD></TR>");
	    }
	    else {
		out.println("<TR><TD align=center>");
		out.println("<table cellSpacing=1 cellPadding=0 width=80% border=0>");
		out.println("<tr>");
		out.println("<td align=center style='BORDER:1px solid'>");
		out.println("<A href="+call_path+"?mid=sign_in style='text-decoration: none;'>");
		out.println(LanguageTrans.getString(session, lid, "Sign In", mhs)+"</A>");
		out.println("</td></tr>");
		out.println("</table>");
		out.println("</TD></TR>");
		out.println("<TR><TD align=center>");
		out.println("<table cellSpacing=1 cellPadding=0 width=80% border=0>");
		out.println("<tr>");
		out.println("<td align=center style='BORDER:1px solid'>");
		out.println("<A href="+call_path+"?mid=join_us style='text-decoration: none;'>");
		out.println(LanguageTrans.getString(session, lid, "Join Us", mhs)+"</A>");
		out.println("</td></tr>");
		out.println("</table>");
		out.println("</TD></TR>");
	    }

	    out.println("</TABLE>");
	    
	    if(directory_table_name == null) {
		return;
	    }

	    Enumeration enum = null; 
	    DirectoryDetails dd = null;

	    Vector v = (Vector)session.getAttribute("DirectoryListVector_"+directory_table_name);

	    if(v == null || v.isEmpty()) {
		try {
		    if(user_profile == null) {
			v = ejbdb.getDirectoryList(directory_table_name, 0);
		    }
		    else {
			v = ejbdb.getDirectoryList(directory_table_name, user_profile.getUserLevel());
		    }
    
		    if(v != null && !v.isEmpty()) {
			enum = v.elements(); 
			while(enum.hasMoreElements()) {
			    dd = (DirectoryDetails)enum.nextElement();
			    if(dd.getParentId().equals("0")) {
				dd.setOpen(false);
				dd.setShow(true);
			    }
			    else {
				dd.setOpen(false);
				dd.setShow(false);
			    }
			}
		    }
		}
		catch (Exception e) {
		    System.out.println("DirectoryServlet:101:"+e);
		}
	    }
	    else {
		enum = v.elements();
		while(enum.hasMoreElements()) {
		    dd = (DirectoryDetails)enum.nextElement();
		    if(did != null && did.equals(dd.getId()) && dd.getType()==0) {
			if(dd.getOpen()) {
			    dd.setOpen(false);
			    v = hiddenMyChildren(v, did);
			}
			else{
			    dd.setOpen(true);
			    v = showMyChildren(v, did);
			}
		    }
		}
	    }

	    if(v != null && !v.isEmpty()) {
		session.setAttribute("DirectoryListVector_"+directory_table_name, v);
	    }
	    else {
		return;
	    }

	    /////////////////////////////////////////////////////////////////////
	    // view 
	    /////////////////////////////////////////////////////////////////////

	    int i = 0;
	    enum = v.elements(); 

	    out.println("<TABLE cellSpacing=0 cellPadding=0 border=0>");
	    out.println("<TR><TD>&nbsp;</TD></TR>");
	    try {
		while(enum.hasMoreElements()) {
		    dd = (DirectoryDetails)enum.nextElement();

		    if(mid != null && mid.equals(dd.getId()+"")) {
			request.setAttribute("mainPageLinkContext", dd.getLinkContext());
			request.setAttribute("mainPageLinkType", ""+dd.getType());
			request.setAttribute("mainPageLink", dd.getLink());
			session.setAttribute("LastDirectoryDetails_"+lid, dd);
		    }

		    if(dd.getShow()) {
			if(dd.getType()==0) {
			    out.println("<TR><TD>");
			    
			    out.println("<table>");
			    out.println("<tr>");
			    for(i=0; i< dd.getPositionX();i++) {
				out.println("<td><img src=/images/final.gif border=0></td>");
			    }
			
			    if(dd.getOpen()) {
				out.println("<td><A href="+call_path+"?lid="+lid+"&cid="+cid+"&did="+dd.getId()+">");
				out.println("<img src=/images/folder_open.gif border=0></A></td>");
			    }
			    else {
				out.println("<td><A href="+call_path+"?lid="+lid+"&cid="+cid+"&did="+dd.getId()+">");
				out.println("<img src=/images/folder.gif border=0></A></td>");
			    }

			    out.println("<td><A href="+call_path+"?lid="+lid+"&cid="+cid+"&mid="+dd.getId());
			    out.println(" style='text-decoration: none;'>");

			    out.println(LanguageTrans.getString(session, lid, dd.getName(), mhs)+"</A>");
			    out.println("</td></tr>");
			    out.println("</table>");
			    out.println("</TD></TR>");
			}
			else {
			    out.println("<TR><TD>");
			    out.println("<table>");
			    out.println("<tr>");
			    
			    for(i=0; i<dd.getPositionX();i++) {
				out.println("<td><img src=/images/final.gif border=0></td>");
			    }

			    out.println("<td><A href="+call_path+"?mid="+dd.getId()+" style='text-decoration: none;'>");
			    out.println(LanguageTrans.getString(session, lid, dd.getName(), mhs)+"</A>");
			    out.println("</td></tr>");
			    out.println("</table>");
			    out.println("</TD></TR>");
			}
		    }
		}
	    }
	    catch (Exception e) {
		System.out.println("DirectoryServlet:Error:301:"+e);
	    }
	    out.println("</TABLE>");
	}
	catch (Exception e) {
	    System.out.println("DirectoryServlet:Exception:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
    {
	doPost(request, response);
    }

    private Vector showMyChildren(Vector v, String id) {
	Vector vps = new Vector();
	vps.add(id);

	DirectoryDetails dd = null;
	Enumeration enum = v.elements();
	while(enum.hasMoreElements()) {
	    dd = (DirectoryDetails)enum.nextElement();
	    if(vps.contains(dd.getParentId())) {
		dd.setShow(true);
		if(dd.getType()==0 && dd.getOpen()) {
		    vps.add(dd.getId());
		}
	    }
	}
	return v;
    }

    private Vector hiddenMyChildren(Vector v, String id) {
	Vector vps = new Vector();
	vps.add(id);

	DirectoryDetails dd = null;
	Enumeration enum = v.elements();
	while(enum.hasMoreElements()) {
	    dd = (DirectoryDetails)enum.nextElement();
	    if(vps.contains(dd.getParentId())) {
		dd.setShow(false);
		vps.add(""+dd.getId());
	    }
	}
	return v;
    }
}








